package com.car.controller.custormer;

import com.car.entity.Custormer;
import com.car.service.custormer.BlackListService;
import com.car.util.PageUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 贾勇
 * @ClassName BlackListController
 * @Description:黑名单列表
 * @DateTime: 2021-12-03 11:15
 */
@RestController
public class BlackListController {

    @Setter
    @Getter
    @Autowired
    private BlackListService blackListService;

    @RequestMapping("/toBaclist")
    public ModelAndView toBaclist() {
        return new ModelAndView("baclist-list");
    }

    /**
     * 获取所有的黑名单
     *
     * @param custormer
     * @return
     */
    @RequestMapping("/getAllBlackList")
    public Map getAllBlackList(int pageNum, int pageSize, Custormer custormer) {

        //创建用于返回和查询得map
        Map<String, Object> map = new HashMap<>();
        //先将对象封装到custormer对象内
        map.put("custormerName", custormer.getCustormerName());
        map.put("custormerPhone", custormer.getCustormerPhone());
        map.put("custormer", custormer);

        //查询记录条数
        Integer count = blackListService.getBlackListCount(map);

        //通过总记录条数和每页大小算出总页数
        Integer total = PageUtil.getTotalPage(count, pageSize);

        //将总页数添加到map中
        map.put("total", total);

        //通过页码和条数算出本次分页的起始位置
        Integer selectstart = pageSize * (pageNum - 1);

        //通过页码和条数算出本次分页的结束位置
        Integer selectEnd = pageNum * pageSize;

        //将起始位置封装到map中，方便后续dao层调用
        map.put("selectstart", selectstart);

        //将结束位置封装到map中，方便后续dao层调用
        map.put("selectEnd", selectEnd);

        //通过map包含的条件 查询符合的custormer组成一个List返回
        List<Custormer> list = blackListService.getAllBlackList(map);

        map.put("list", list);

        return map;
    }

    /**
     * 跳转到增加黑名单页面
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        return new ModelAndView("baclist-add");
    }

    /**
     * 增加黑名单的请求
     *
     * @param custormer
     * @return
     */
    @RequestMapping("/addBlackList")
    public String addBlackList(Custormer custormer) {

        Integer integer = blackListService.addBlackList(custormer);
        return integer.toString();

    }

    /**
     * 删除请求
     *
     * @param custormerId
     * @return
     */
    @RequestMapping("/delBlackList")
    public String delBlackList(@RequestParam("custormerId") int custormerId) {

        Integer integer = blackListService.delBlackList(custormerId);

        return integer.toString();

    }

    /**
     * 跳转到修改页面
     *
     * @param custormerId
     * @param
     * @return
     */
    @RequestMapping("/toupdate")
    public ModelAndView toupdate(int custormerId) {

        ModelAndView mv = new ModelAndView();
        Custormer cusId = blackListService.getById(custormerId);
        mv.addObject("cusId", cusId);
        mv.setViewName("baclist-edit");
        return mv;
    }

    /**
     * 修改页面请求
     *
     * @param custormer
     * @return
     */
    @RequestMapping("/update")
    public String update(Custormer custormer) {

        Integer integer = blackListService.updateBlackList(custormer);

        return integer.toString();
    }

}
